package components.editbar
{
	import capricornus.components.ComponentsManager;
	import capricornus.events.StageEvent;
	import capricornus.core.StageManager;
	
	import components.map.MapController;
	import components.toolbar.ToolBarController;
	import components.utils.file.FileManager;
	
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class EditBarController
	{
		public static var instance:EditBarController;
		
		private var _view:EditBarView;
		
		public function EditBarController()
		{
			init();
		}
		
		private function init():void
		{
			instance = this;
			
			_view = new EditBarView( ComponentsManager.instance.foreLayer );
			_view.show();
			
			_view.txtRate.mouseEnabled = false;
//			_view.srScale.liveDragging = true;
			_view.group.addEventListener( Event.CHANGE, onClickGroup, false, 0, true );
			_view.srScale.addEventListener( Event.CHANGE, onScaleChange, false, 0, true );
			_view.srLine.addEventListener( Event.CHANGE, onGridChange, false, 0, true );
			_view.srGrid.addEventListener( Event.CHANGE, onGridChange, false, 0, true );
			_view.nsPen.addEventListener( Event.CHANGE, onPenChange, false, 0, true );
			_view.cbPenType.addEventListener( Event.CHANGE, onPetTypeChange, false, 0, true );
			_view.btnSelect.addEventListener( MouseEvent.CLICK, onClickSelect, false, 0, true );
			
			_view.cbGrid.addEventListener( Event.CHANGE, onVisibleChange, false, 0, true );
			_view.cbItem.addEventListener( Event.CHANGE, onVisibleChange, false, 0, true );
			
			onResize( null );
			StageManager.instance.addEventListener( StageEvent.RESIZE, onResize, false, 0, true );
		}
		
		public function setMouseInfo( x:int, y:int, i:int, j:int ):void
		{
			_view.txtX.text = x.toString();
			_view.txtY.text = y.toString();
			_view.txtI.text = i.toString();
			_view.txtJ.text = j.toString();
		}
		
		private function onClickGroup( e:Event ):void
		{
			var index:int = int(_view.group.selection.name.charAt(2));
			MapController.instance.editType = index;
		}
		
		private function onScaleChange( e:Event ):void
		{
			_view.txtRate.text = _view.srScale.value.toString();
			MapController.instance.scale = _view.srScale.value;
		}
		
		private function onGridChange( e:Event ):void
		{
			MapController.instance.setGridAlpha( e.currentTarget==_view.srGrid?_view.srGrid.value:-1, e.currentTarget==_view.srLine?_view.srLine.value:-1 );
		}
		
		private function onVisibleChange( e:Event ):void
		{
			if( e.currentTarget==_view.cbGrid )
			{
				MapController.instance.setGridVisible( _view.cbGrid.selected );
			}
			else
			{
				MapController.instance.setItemVisible( _view.cbItem.selected );
			}
		}
		
		private function onResize( e:StageEvent ):void
		{
			_view.setLocation( ToolBarController.instance.width, 0 );
			_view.back.width = StageManager.stageWidth - ToolBarController.instance.width;
		}
		
		private function onPenChange( e:Event ):void
		{
			MapController.instance.penSize = _view.nsPen.value;
		}
		
		private function onPetTypeChange( e:Event ):void
		{
			MapController.instance.penType = _view.cbPenType.selected;
		}
		
		private function onClickSelect( e:MouseEvent ):void
		{
			if( MapController.instance.isMapCreated )
			{
				FileManager.instance.selectBackgoundImage();
			}
		}
	}
}















